<?php

namespace app\common\model\printmanager;

use think\Model;


class Order extends Model
{

    

    

    // 表名
    protected $name = 'self_order';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'shipping_type_text',
        'status_text',
        'createtime_text',
        'paytime_text',
    ];

    public static function init()
    {
        self::event('after_insert', function ($order) {
            $order = self::get($order->id);
            $order->real_amount = $order['amount'] + $order['shipping_price'] - $order['gold_amount'];
            $order->save();
        });

        self::event('after_update', function ($order) {
            $order = self::get($order->id);
            $order->real_amount = $order['amount'] + $order['shipping_price'] - $order['gold_amount'];
            $order->save();
        });
    }
    

    
    public function getShippingTypeList()
    {
        return ['0' => "物流配送", '1' => "自提"];
    }

    public function getStatusList()
    {
        return ['0' => "待付款", '1' => "待发货", '2' => "待收货", '3' => "已收货", '4' => "已取消", '5' => "已完成"];
    }


    public function getShippingTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['shipping_type']) ? $data['shipping_type'] : '');
        $list = $this->getShippingTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getOrderGoodsAttr($value, $data)
    {
        $order_id = isset($data['id']) ? $data['id'] : '';
        $res = [];
        if($order_id){
            $res = \think\Db::name('self_order_goods')->cache(60)->where('order_id', $order_id)->select();
            foreach ($res as $key => &$value) {
                $info = Goods::cache(false)->field('id,name,introduce, images')->find($value['goods_id']);
                $info->file_list = $info->file_list;
                $value['info'] = $info;
            }
        }
        return $res;
    }

    public function getAddressAttr($value, $data)
    {
        $address_id = isset($data['address_id']) ? $data['address_id'] : 0;
        return $address_id ? \app\common\model\Address::find($address_id) : null;
    }

    public function getUserInfoAttr($value, $data)
    {
        $user_id = isset($data['user_id']) ? $data['user_id'] : 0;
        return $user_id ? \app\common\model\User::getBaseInfoNosense($user_id) : null;
    }

    public function getCreatetimeTextAttr($value, $data)
    {
        $createtime = isset($data['createtime']) ? $data['createtime'] : 0;
        return $createtime ? date("Y-m-d H:i:s", $createtime) : '';
    }

    
    public function getPaytimeTextAttr($value, $data)
    {
        $createtime = isset($data['paytime']) ? $data['paytime'] : 0;
        return $createtime ? date("Y-m-d H:i:s", $createtime) : '';
    }
}
